package com.piesat.quake.common.mqtt.listener;

import com.piesat.quake.business.device.bis.MqttHelper;
import com.piesat.quake.business.station.service.StationInfoService;
import com.piesat.quake.common.mongo.service.MongoService;
import com.piesat.quake.common.mqtt.MqttGateway;
import com.piesat.quake.common.mqtt.decoder.FdDataDecoder;
import com.piesat.quake.common.mqtt.decoder.ReDataDecoder;
import com.piesat.quake.common.mqtt.decoder.WcDataDecoder;
import com.piesat.quake.common.mqtt.decoder.common.CommonEncoder;
import com.piesat.quake.common.mqtt.decoder.entity.FdData;
import com.piesat.quake.common.mqtt.decoder.entity.ReData;
import com.piesat.quake.common.mqtt.decoder.entity.WcData;
import com.piesat.quake.common.mqtt.entity.MqttEvent;

import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class JobListener {

    private final MqttGateway gateway;

    private final StationInfoService stationInfoService;
    private final MongoService mongoService;

    private final SimpMessagingTemplate template;

    /**
     * 注册报文
     *
     * @param mqttEvent
     * @throws Exception
     */
//    @EventListener(condition = "#mqttEvent.topic.endsWith('register/data')")
    public void onMqttCallRegister(MqttEvent mqttEvent) throws Exception {
        log.info("接收到注册报文：{}", mqttEvent.getMessage());
        MqttHelper.setVersion(mqttEvent.getTopic());
        //解析注册报文，返回注册成功
        ReDataDecoder decoder = new ReDataDecoder(mqttEvent.getMessage());
        ReData data = decoder.decoder();
        //更新站点信息
        stationInfoService.registerStation(data);
        //回复注册成功
        gateway.sendToMqtt("vibrate/" + data.getId() + "/register/control", CommonEncoder.encoder(16, "rr", (int) 16, data.getId(), (short) 0));
    }

    /**
     * 一般特征报文
     *
     * @param mqttEvent
     */
    @EventListener(condition = "#mqttEvent.topic.contains('feature/data')")
    public void onMqttCallFeature(MqttEvent mqttEvent) {
//        log.info("接收到一般特征报文：{}", mqttEvent.getMessage());
        byte[] message = mqttEvent.getMessage();
        FdDataDecoder decoder = new FdDataDecoder(message);
        FdData data = decoder.decoder();
        template.convertAndSend("/topic/fd", data);
        mongoService.save(data);
//        webSocket.sendAllTopicMessage("fd", JSONObject.toJSONString(data));
    }

    /**
     * 实时数据报文
     *
     * @param mqttEvent
     */
    @EventListener(condition = "#mqttEvent.topic.contains('ontime/data')")
    public void onMqttCallOnTime(MqttEvent mqttEvent) {
//        log.info("接收到实时数据报文：{}", mqttEvent.getMessage());
        WcDataDecoder decoder = new WcDataDecoder(mqttEvent.getMessage());
        WcData data = decoder.decoder();
log.info("WcData:{}",data);
        template.convertAndSend("/topic/wc", data);
        mongoService.save(data);
//        webSocket.sendAllTopicMessage("wc", JSONObject.toJSONString(data));
    }

    @EventListener(condition = "#mqttEvent.topic.matches('$SYS/brokers/+/clients/+/connected')")
    public void onMqttConnected(MqttEvent mqttEvent) {
        log.info("设备连接：{}", mqttEvent.getMessage());
    }

    @EventListener(condition = "#mqttEvent.topic.matches('$SYS/brokers/+/clients/+/disconnected')")
    public void onMqttDisconnected(MqttEvent mqttEvent) {
        log.info("设备断开连接：{}", mqttEvent.getMessage());
    }

}
